#pragma once
#include<iostream>
#include<assert.h>
using namespace std;


namespace bit {
	template<class T>
	struct list_node
	{
		T _val;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& val=T())
			:_val(val)
			,_next(nullptr)
			,_prev(nullptr)
		{ }
	};

	template<class T>
	class __list_iterator
	{
	public:
		typedef list_node<T> Node;

		Node* _node;

		__list_iterator(Node *node)
			:_node(node)
		{ }

		__list_iterator<T>& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		bool operator!=(const __list_iterator<T>& it)
		{
			return _node != it._node;
		}

		T& operator*()
		{
			return this->_node->_val;
		}


	};

	template<class T>
	class list {
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T> iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}


		void push_back(const T& val)
		{
			Node* newnode = new Node(val);
			Node* tail = _head->_prev;

			newnode->_next = _head;
			newnode->_prev = tail;

			tail->_next = newnode;
			_head->_prev = newnode;
		}

		list(const T val = T())
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void push_front(const T& val)
		{
			Node* newnode = new Node(val);

			newnode->_next = _head->_next;
			newnode->_prev = _head->_prev;

			_head->_next = newnode;
		}



	private:
		Node* _head;

	};

	void print(list <int>& l)
	{
		bit::list<int>::iterator i = l.begin();
		while (i != l.end())
		{
			cout << *i << " ";
			++i;
		}
		std::cout << endl;
	}

	void push_back(list <int>& l, size_t start,  int num = 1)
	{
		assert(num > 0);
		for (size_t i = start; i < start + num; i++)
		{
			l.push_back(static_cast<int>(i)); 
		}
	}
	void push_front(list <int>& l, size_t start, int num = 1)
	{
		assert(num > 0);
		for (size_t i = start; i < start + num; i++)
		{
			l.push_front(static_cast<int>(i));
		}
	}

	void test_my_list()
	{
		list<int>l;

		push_back(l, 10, 10);

		l.push_front(10);
		l.push_front(100);
		push_front(l, 10, -100);
		print(l);
	}

}